package li.com.springaidemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import li.com.springaidemo.mapper.MessageMapper;
import li.com.springaidemo.pojo.MessageLog;
import li.com.springaidemo.service.MessageService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessageLog> implements MessageService {
    @Override
    public void saveto(MessageLog messageLog) {
        QueryWrapper<MessageLog> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userid",messageLog.getUserid()).eq("personaname",messageLog.getPersonaname());
        Long l = this.baseMapper.selectCount(queryWrapper);
        if(messageLog.getUserid()!=1){
            if(l>=100){
                queryWrapper.orderByAsc("time");
                List<MessageLog> list = super.list(queryWrapper);
                for (int i=0;i<10;i++) {
                    MessageLog messageLog1 = list.get(i);
                    this.baseMapper.deleteById(messageLog1);
                }
            }
        }
        super.save(messageLog);
    }
}